<template>
	<div id="tmpl">
		<div id="postcomment">
			<h4>提交评论</h4>
			<hr/>
			<textarea placeholder="请输入评论内容" v-model="postcontent" @keydown.enter="postcomment"></textarea>
			<mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
		</div>
		<div id="list">
			<h4>评论列表</h4>
			<hr/>
			<div v-for="(item,index) in list">
				<div class="title">
					<span>第{{index + 1}}楼</span>
					<span>用户：{{item.user_name}}</span>
					<span>发表时间：{{item.add_time|datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
				</div>
				<ul class="mui-table-view">
					<li class="mui-table-view-cell" v-text="item.content"></li>
				</ul>
			</div>
		</div>
		<!-- 获取更多按钮 -->
		<mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
	</div>
</template>
<script>
import common from '../../kits/common.js';
import {Toast} from 'mint-ui';
	export default{
		props:['id'],
		data(){
			return{
				postcontent:'',
				list:[],
				pageindex:1,
			}
		},
		created(){this.getcommentlist(this.pageindex)},
		methods:{
			getmore(){
				this.pageindex++;
				this.getcommentlist(this.pageindex);
			},
			getcommentlist(pageindex){
				pageindex = pageindex || 1;
				var url=common.apidomain+"/api/getcomments/"+this.id+"?pageindex="+pageindex;
				this.$http.get(url).then(function(response){
					if (response.body.status!=0) {
						Toast(response.body.message);
						return;
					}
					//this.list=response.body.message.concat(this.list);
					this.list=this.list.concat(response.body.message)
				})
			},
			postcomment(){
				if (this.postcontent.trim().length<=0) {
					Toast('请先输入评论内容！');
					return;
				}
				var url=common.apidomain+"/api/postcomment/"+this.id;
				this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then(function(response){				Toast(response.body.message);
					//将最新评论数据追加到评论列表的最顶部
					this.list=[{'user_name':'不是匿名的用户','add_time':new Date(),'content':this.postcontent}].concat(this.list);		
					this.postcontent='';
				})
				//this.getcommentlist(1);
			}
		}

	}
</script>
<style scoped>
	#postcomment {
		padding: 5px;	
	}
	hr {
		opacity: 0.4;
		margin: 5px 0;
	}
	textarea {
		height: 150px;
	}
	#list{
		padding: 5px;
	}
	#list .title{
		padding: 5px;
		color: #6d6d72;
		font-size: 15px;
		background-color: rgba(0,0,0,0.1);
	}
</style>